// 命名空间
var hall_common_module = hall_common_module || {};

// 所有代码
hall_common_module.path = 'modules/main/hall_common_module/';
hall_common_module.srcPath = hall_common_module.path + 'src';
hall_common_module.src = [
    //小功能  红包 礼券 背包
    hall_common_module.srcPath + "/ProtobufferTables.js",  //protobuf的返回code
    hall_common_module.srcPath + "/ItemUtils.js", //道具维护
    //红包
    hall_common_module.srcPath + "/RedBagLayer/RedBagLayer.js",
    hall_common_module.srcPath + "/RedBagLayer/RedBagWindow.js",
    hall_common_module.srcPath + "/RedBagLayer/RecordLayer.js",
    //背包
    hall_common_module.srcPath + "/BagLayer/BagLayer.js",
    hall_common_module.srcPath + "/BagLayer/BagWindow.js",
    //活动公告
    hall_common_module.srcPath + "/activity/ActivityWindow.js",
    hall_common_module.srcPath + "/activity/ActivityLayer.js",
    //礼券商城
    hall_common_module.srcPath + "/TicketShopLayer/TicketShopLayer.js",
    hall_common_module.srcPath + "/TicketShopLayer/TicketShopWindow.js",
    hall_common_module.srcPath + "/TicketShopLayer/TicketShopRecord.js",
    //签到
    hall_common_module.srcPath + "/SignLayer/SignInCfg.js",
    hall_common_module.srcPath + "/SignLayer/SignLayer.js",
    hall_common_module.srcPath + "/SignLayer/SignWindow.js",
    //转盘
    hall_common_module.srcPath + "/TurnTable/TurnTableCfg.js",
    hall_common_module.srcPath + "/TurnTable/TurnTableLayer.js",
    hall_common_module.srcPath + "/TurnTable/TurnTableWindow.js",
    //物品奖励弹出框
    hall_common_module.srcPath + "/ShowGoodsLayer.js",
    //邮件
    hall_common_module.srcPath + "/EmailLayer/EmailLayer.js",
    hall_common_module.srcPath + "/EmailLayer/EmailWindow.js",
    hall_common_module.srcPath + "/EmailLayer/EmailShowContent.js",
];

// 所有资源
hall_common_module.resPath = hall_common_module.path + 'res/';
hall_common_module.res = {
    RedBagLayer_json: hall_common_module.resPath + 'otherFunc/RedBagLayer.json',
    RedBagWindow_json: hall_common_module.resPath + 'RedBagWindow.json',
    RecordLayer_json: hall_common_module.resPath + 'otherFunc/RecordLayer.json',
    Record_item_json: hall_common_module.resPath + 'otherFunc/Record_item.json',
    BagLayer_json: hall_common_module.resPath + 'otherFunc/bagLayer.json',
    BagWindow_json: hall_common_module.resPath + 'BagWindow.json',
    ActivityWindow_json: hall_common_module.resPath + 'ActivityWindow.json',
    BagItem_json: hall_common_module.resPath + 'otherFunc/bag_item.json',
    ActivityLayer_json: hall_common_module.resPath + 'otherFunc/activity_layer.json',
    TicketShop_Item_json: hall_common_module.resPath + 'otherFunc/TicketShop_Item.json',
    TicketShopLayer_json: hall_common_module.resPath + 'otherFunc/TicketShopLayer.json',
    TicketShopWindow: hall_common_module.resPath + 'TicketShopWindow.json',
    SignInLayer_json: hall_common_module.resPath + 'sign/SignInLayer.json',
    SignWindow: hall_common_module.resPath + 'SignWindow.json',
    TurnTableLayer_json: hall_common_module.resPath + 'turnTable/turnTableLayer.json',
    TurnTableWindow: hall_common_module.resPath + 'TurnTableWindow.json',
    ShowGoodsLayer_json: hall_common_module.resPath + 'showGoods/showGoodsLayer.json',
    EmailLayer_json: hall_common_module.resPath + "otherFunc/email_layer.json",
    EmailItem_json: hall_common_module.resPath + "otherFunc/email_item.json",
    EmailContent_json: hall_common_module.resPath + "otherFunc/email_content.json",
    EmailWindow_json: hall_common_module.resPath + "EmailWindow.json",



    img_not_buy: hall_common_module.resPath + "otherFunc/ticketShop/img_not_buy.png",
    img_can_buy: hall_common_module.resPath + 'otherFunc/ticketShop/img_can_buy.png',





};


/*
 * 公共奖励弹窗
 * data 数组结构
 * itemname itemurl 2项必须  itemval 可选(因为有些itemname中带有数量)
 * scale 默认1 物品缩放比例 默认图大小为100 x 100
 */

var showGoodsTips = function(data, scale, cb){
    var goodsTipsLayer =  new ShowGoodsLayer(data, scale, cb);
    cc.director.getRunningScene().addChild(goodsTipsLayer);
};

//下载缓存图，回调自己实现所需逻辑
var downLoadImage = function (url, cb) {
    if(!url || url == '')
        return;
    var md5_url = Crypto.MD5(url);

    var saveImage = function (data) {
        if (data) {
            var dir_path = jsb.fileUtils.getWritablePath() + 'headimage/';
            var file_path = dir_path + md5_url + '.jpg';
            if (!jsb.fileUtils.isDirectoryExist(dir_path)) {
                jsb.fileUtils.createDirectory(dir_path);
            }
            if (jsb.fileUtils.writeDataToFile(data, file_path)) {
                cb(file_path);
            } else {
                asyncDownloader();
                console.log('Remote write file failed.');
            }
        } else {
            asyncDownloader();
            console.log('Remote download file failed.');
        }
    };

    var asyncDownloader = function () {
        cc.textureCache.addImageAsync(url, function (texture) {
            cb(texture);
        }, null);
    };

    var httpAsynDownloader = function () {
        var xhr = cc.loader.getXMLHttpRequest();
        xhr.responseType = 'arraybuffer';
        xhr.onreadystatechange = function () {
            console.log('xhr.readyState: ' + xhr.readyState);
            console.log('xhr.status:' + xhr.status);
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    saveImage(new Uint8Array(xhr.response));
                } else {
                    asyncDownloader();
                }
            }
        }.bind(this);
        xhr.open('GET', url, true);
        xhr.send();
    };

    var loadImageFile = function () {
        if (cc.sys.isNative) {
            var file_path = jsb.fileUtils.getWritablePath() + 'headimage/' + md5_url + '.jpg';
            if (jsb.fileUtils.isFileExist(file_path)) {
                cb(file_path);
            } else {
                httpAsynDownloader();
            }
        } else {
            asyncDownloader();
        }
    };

    loadImageFile();
};